/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package oldfiles;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;


/**
 *
 * @author Don
 */
public class ServletAddCustomer extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
          
           // String sqlinject = "[a-zA-Z'.\s | \d | \- | \/ | \$ | \£ | \€ | \( | \) | \ | \! | \% | \+ | \& | \, | \! $]{1,200}$";
           // Pattern pattern = Pattern.compile(sqlinject);
            //Matcher matcher = pattern.matcher(request.getParameter("email"));  
           // while (matcher.find())
          //  {
          //      response.sendRedirect("generic_home.jsp");
          //  }
            
          //  if(request.getParameter("email").contains("'"))
          // {
         //       response.sendRedirect("generic_home.jsp");
          //      System.out.println("hello");
          //  }
            
            
            String houseno;
            String street;
            String subdivision;
            String city;
            String postalcode;
            String country;
            
            Customer newCustomer = new Customer();
  
            
            newCustomer.setEmail(request.getParameter("email"));
            newCustomer.setUsername(request.getParameter("username"));
            newCustomer.setPassword(request.getParameter("password"));
            newCustomer.setFirstname(request.getParameter("first_name"));
            newCustomer.setMiddlename(request.getParameter("middle_name"));
            newCustomer.setLastname(request.getParameter("last_name"));
            newCustomer.setCreditcardname(request.getParameter("credit_card_name"));
            newCustomer.setCreditcardnumber(request.getParameter("credit_card_number"));
            newCustomer.setCreditcardtype(request.getParameter("credit_card_type"));
            newCustomer.setCreditcardexpdate(request.getParameter("credit_card_exp_date"));
            
           
            //newCustomer.setHouseno(request.getParameter("house_number"));
            //newCustomer.setStreet(request.getParameter("street"));
            //newCustomer.setSubdivision(request.getParameter("subdivision"));
           // newCustomer.setCity(request.getParameter("city"));
           // newCustomer.setPostalcode(request.getParameter("postal_code"));
           // newCustomer.setCountry(request.getParameter("country"));
            
            houseno = request.getParameter("house_number");
            street = request.getParameter("street");
            subdivision = request.getParameter("subdivision");
            city = request.getParameter("city");
            postalcode = request.getParameter("postal_code");
            country = request.getParameter("country");
            
            
            newCustomer.setAddress(houseno +" "+ street +" "+ subdivision +" "+ city +" "+ postalcode +" "+ country);
            
            
            boolean result = new CustomerManager().addCustomer(newCustomer);
            
            if(result)
                response.sendRedirect("generic_home.jsp");
            else
                response.sendRedirect("error_page.jsp");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
